<?php
/**
 * 权限
 */
namespace app\model;

use think\Model;
use think\facade\Db;


class Role extends Model
{
   /**
    * 角色详情
    *
    */
   public function detail($id){
      $role = Role::find($id);
      return $role;
   }
  /**
   * 添加
   *
  */
   public function AddData($param){
     
      $role = new Role;
      $data=[];
      $data['role_name']=$param['role_name'];
      $data['create_time']=date('Y-m-d H:i:s');
      $data['city_id']=$param['city_id'];
      if(isset($param['park_id'])&&!empty($param['park_id'])){
      	$data['park_id']=$param['park_id'];
      }
      $data['chain_name']=$param['chain_name'];
      $data['copyright']=$param['copyright'];
      $role->save($data);
      return $role->id; 
   }
 /**
   * 编辑
   *
  */
  public function EditData($param,$id){
   $role = Role::find($id);
   $data['role_name']=$param['role_name'];
   $data['city_id']=$param['city_id'];
   if(isset($param['park_id'])&&!empty($param['park_id'])){
   	 $data['park_id']=$param['park_id'];
   }
   $data['chain_name']=$param['chain_name'];
   $data['copyright']=$param['copyright'];
   $role->save($data);
   return $id; 
  }
  /**
   * 修改禁用状态
   *
  */
  public function UpdateStatus($id){
   $role = Role::find($id);
   $role->status=$role->status==1?0:1;
   $role->save();
   return true; 
  }
 /**
   * 删除
   *
  */
  public function Del($param){
   $update=[];
   foreach($param as $val){
       $update[]=['id'=>$val,'status'=>-1,'delete_time'=>date('Y-m-d H:i:s')];
   }
   if(!empty($update)){
      $role = new Role;
      $role->saveAll($update);
   }
   return true; 
  }
 /**
   * 设置默认
   *
  */
  public function SetDefault($id){
     $role = Role::find($id);
     if(empty($role)||$role['status']==-1){
       return ['status'=>0,'msg'=>'当前角色不存在'];
     }
     $default=$role->is_default;
     if($default==1){
        return ['status'=>0,'msg'=>'至少设置一个默认'];
     }
      
      Role::update(['is_default' => 0], ['is_default' => 1]);
      Role::update(['is_default' => 1], ['id' => $id]);
      return ['status'=>1,'msg'=>'设置成功'];
  }
  /**
   * 获取权限列表
   */
  public function GetMenu($where,$user_id){
    
     $where.=" and a.status=1 ";
     
     //最大管理员
     if($user_id==1||1==1){
     	$res= Db::name('menu')->alias('a')->field('a.id as menu_id,a.menu_name,a.url,a.icon,a.pid')->where($where)->order('sort desc')->paginate(50);  
        $result=arr2tree($res->items(),0,'menu_name','menu_id');
        
        return ['total'=>$res->total(),'data'=>$result];
     }


     $where.=" and b.status=1 ";
     $role_ids=Db::name('admin_role')->where(['status'=>1,'admin_id'=>$user_id])->column('role_id');
     
     if(!empty($role_ids)){
        $role_ids_str=implode(',',$role_ids);
        $where.=" and b.role_id in ($role_ids_str)";
     }else{
        $where.=" and 1=2 ";
     }

     $res= Db::name('menu')->alias('a')->field('a.id as menu_id,a.menu_name,a.url,a.icon,a.pid')->join('role_menu b','a.id=b.menu_id','left')->where($where)->order('sort desc')->paginate(50);
    
     $result=arr2tree($res->items(),0,'menu_name','menu_id');
     
     return ['total'=>$res->total(),'data'=>$result];
  }
  /**
   * 通过user_id获取role_name
   */
  public function GetRoleNameByUserId($userid){
  	$role_name=Role::alias('a')->join('admin_role b','a.id=b.role_id','left')->where(['b.admin_id'=>$userid,'b.status'=>1])->value('a.role_name');
  	return $role_name;
  }
}